//
//  ChatMessageSender.swift
//  Mixin
//
//  Created by zhanghanbing on 2018/8/29.
//  Copyright © 2018年 Insight. All rights reserved.
//

import RxSwift

class ChatMessageSender {
    fileprivate let chat: OCTChat
    
    init(chat: OCTChat) {
        self.chat = chat
    }
    
    public func sendMessage(_ text: String) -> Observable<OCTMessageAbstract?> {
        return Observable.create { observer -> Disposable in
            ToxManager.shared.toxMananger!.chats.sendMessage(to: self.chat, text: text, type: .normal, successBlock: { msg in
                observer.onNext(msg)
                observer.onCompleted()
            }, failureBlock: nil)
            
            return Disposables.create {
                ToxManager.shared.toxMananger!.objects.change(self.chat, enteredText: "")
            }
        }
    }
    
    public func sendImage(_ image: UIImage, _ fileName: String? = nil) {
        guard let data = UIImageJPEGRepresentation(image, 0.9) else {
            return
        }
        
        var fileName = fileName
        if fileName == nil {
            
        }
        
        let submanagerFiles = ToxManager.shared.toxMananger!.files
        submanagerFiles.send(data, withFileName: fileName!, to: chat) { (error: Error) in
        }
    }
}
